package com.lx.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lx.yygh.hosp.repository.DepartmentRepository;
import com.lx.yygh.hosp.service.DepartmentService;
import com.lx.yygh.model.hosp.Department;
import com.lx.yygh.model.hosp.Schedule;
import com.lx.yygh.vo.hosp.DepartmentVo;
import com.mongodb.client.result.DeleteResult;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author c先生
 * @date 2022年10月16日 下午 5:33:49
 * @describe
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    DepartmentRepository departmentRepository;
    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public Page findPageDepartment(Integer page, Integer limit, Department department) {
        //pageable对象
        Pageable pageable= PageRequest.of(page-1,limit);
        //example:构建条件查询对象
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase();
        Example<Department> example = Example.of(department,matcher);
        Page pageModel = departmentRepository.findAll(example, pageable);
        return pageModel;
    }

    @Override
    public Boolean saveDepartment(Map<String, Object> map) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(map), Department.class);
        departmentRepository.save(department);
        return true;
    }

    @Override
    public Boolean deleteDepartment(String depcode) {
        Query query = new Query();
        query.addCriteria(Criteria.where("depcode").is(depcode));
        DeleteResult deleteResult = mongoTemplate.remove(query, Department.class);
        return deleteResult.wasAcknowledged();
    }

    @Override
    public List<DepartmentVo> findDeptDataTree(String hoscode) {
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院的编号获取所有的科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        List<Department> allDepartment = departmentRepository.findAll(example);

        //根据大科室进行分组
        Map<String, List<Department>> map = allDepartment.stream().collect(Collectors.groupingBy(Department::getBigcode));
        Set<Map.Entry<String, List<Department>>> entries = map.entrySet();
        for (Map.Entry<String, List<Department>> entry : entries) {
            //获取一个大科室的编号
            String bigDepartmentCode = entry.getKey();
            //获取该大科室下的所有小科室
            List<Department> smallDepartmentList = entry.getValue();
            //封装大科室
            DepartmentVo bigDepartmentVo = new DepartmentVo();
            bigDepartmentVo.setDepcode(bigDepartmentCode);
            bigDepartmentVo.setDepname(smallDepartmentList.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : smallDepartmentList) {
                DepartmentVo smallDepartmentVo = new DepartmentVo();
                smallDepartmentVo.setDepcode(department.getDepcode());
                smallDepartmentVo.setDepname(department.getDepname());
                children.add(smallDepartmentVo);
            }
            bigDepartmentVo.setChildren(children);
            result.add(bigDepartmentVo);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
